package com.ycsj.goldmedalnewconcept.student.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.student.adapter.TableRvAdapter;
import com.ycsj.goldmedalnewconcept.student.bean.TableInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 学生分析界面
 * Created by Administrator on 2019/2/19 0019.
 */

public class StudentAnalyzeActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //数据集合
    private List<TableInfo.DataBean> oldDatas;
    //适配器
    private TableRvAdapter adapter;

    //携带的班级号,toltal,单元号
    private String classNum, total, unit;

    //题库练习携带过来的位置
    private int position;

    //传递的TkItemBean对象
    private TkItemBean tkItemBean;
    //空视图
    private View emptyView;

    //刷新该界面的标识
    public static final int UPDATEPAGE = 0x880;


    /**
     * 跳转到当前页面的方法
     *
     * @param context
     */
    public static void startActivity(Context context, String classNum, String total, String unit, int postion) {
        Intent intent = new Intent(context, StudentAnalyzeActivity.class);
        intent.putExtra("classnum", classNum);
        intent.putExtra("total", total);
        intent.putExtra("unit", unit);
        intent.putExtra("postion", postion);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_student_analyze;
    }

    @Override
    protected void init() {
        initStateBar();

        //初始化标题栏
        initTitle();

        initIntent();

        //初始化列表
        initRv();

        //请求服务器数据
        http(BqaManager.NORML);

    }


    /**
     * 获取传递的参数
     */
    private void initIntent() {
        classNum = getIntent().getStringExtra("classnum");
        total = getIntent().getStringExtra("total");
        unit = getIntent().getStringExtra("unit");
        position = getIntent().getIntExtra("postion", -1);

    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("学生分析");
    }

    /**
     * 初始化表格列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        adapter = new TableRvAdapter(this, position, unit, this, R.layout.teacher_app_rv_tiem_student_analyze, oldDatas);
    }

    /**
     * 请求服务器数据
     */
    private void http(final int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "11");
        map.put("ttel", AccountUtils.getAccount(this));//老师账号
        map.put("classnum", classNum);
        map.put("unit", unit);
        map.put("total", total);

        HttpUtils.post(HttpAddress.STUDENTANALYZE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoading, null, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (llContent != null && llContent.getVisibility() != View.VISIBLE) {
                    llContent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    TableInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TableInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<TableInfo.DataBean> newDatas = info.getData();
                            if (newDatas != null) {
                                if (method == BqaManager.NORML) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);
                                    BqaManager.setRv(emptyView, StudentAnalyzeActivity.this, adapter, rv);
                                }
                            }
                        } else {
                            ToastUtils.showShort(Constants.ERRORSTATE + info.getState());
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back})
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }

    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && busInfo.type == UPDATEPAGE) {
            http(BqaManager.NORML);
        }
    }
}
